package com.mk.common;

/**
 * Created by yanzhiping on 2017/8/15.
 */
public class ProtocolUtil {
    public static char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

    public static byte charToByte(char i) {
        return (byte) "0123456789ABCDEF".indexOf(i);
    }

    public static byte[] hexStrToBytes(String hex) {
        if (hex != null && !"".equals(hex)) {
            hex = hex.toUpperCase();
            int left = hex.length() % 2;
            int size = hex.length() / 2;
            byte[] bs = new byte[size + left];
            for (int i = 0; i < size; i++) {
                int a = charToByte(hex.charAt(2 * i));
                int b = charToByte(hex.charAt(2 * i + 1));
                bs[i] = (byte) (a << 4 | b);
            }
            if (left == 1)
                bs[size] = charToByte(hex.charAt(hex.length() - 1));
            return bs;
        }
        return null;
    }

    public static String byteArrToHexStr(byte[] arr) {
        if (arr != null) {
            String res = "";
            for (int i = 0; i < arr.length; i++) {
                res += byteToHexStr(arr[i]);
            }
            return res;
        }
        return null;
    }

    public static String byteToHexStr(byte b) {
        int a = (0xff & b) / 16;
        int c = (0xff & b) % 16;
        return hexChars[a] + "" + hexChars[c];
    }

    public static String getMacHex(long mac) {
        String re = "";
        for (int i = 0; i < 5; i++) {
            re += byteToHexStr((byte) (mac >> (8 * (5 - i))));
        }
        re += byteToHexStr((byte) (mac));
        return re;
    }

    public static long getMacLong(String mac) {
        return Long.parseLong(mac, 16);
    }

    public static void putByteToBuffer(byte[] bs, byte b, int index) {
        bs[index] = b;
    }

    public static void putShortToBuffer(byte[] bs, short s, int index) {
        putByteArrToBuffer(bs, shortToByteArr(s), index);
    }

    public static void putIntToBuffer(byte[] bs, int i, int index) {
        putByteArrToBuffer(bs, intToByteArr(i), index);
    }

    public static void put8LongToBuffer(byte[] bs, long l, int index) {
        putByteArrToBuffer(bs, longTo8LenByteArr(l), index);
    }

    public static void put6LongToBuffer(byte[] bs, long l, int index) {
        putByteArrToBuffer(bs, longTo6LenByteArr(l), index);
    }

    public static void putByteArrToBuffer(byte[] bs, byte[] toAddArr, int index) {
        for (int i = 0, l = toAddArr.length; i < l; i++) {
            bs[index + i] = toAddArr[i];
        }
    }

    public static byte[] shortToByteArr(short s) {
        byte[] bs = new byte[2];
        bs[0] = (byte) (s >> 8);
        bs[1] = (byte) s;
        return bs;
    }

    public static byte[] intToByteArr(int i) {
        byte[] bs = new byte[4];
        bs[0] = (byte) (i >> 24);
        bs[1] = (byte) (i >> 16);
        bs[2] = (byte) (i >> 8);
        bs[3] = (byte) i;
        return bs;
    }

    public static byte[] longTo6LenByteArr(long l) {
        byte[] bs = new byte[6];
        bs[0] = (byte) (l >> 40);
        bs[1] = (byte) (l >> 32);
        bs[2] = (byte) (l >> 24);
        bs[3] = (byte) (l >> 16);
        bs[4] = (byte) (l >> 8);
        bs[5] = (byte) l;
        return bs;
    }

    public static byte[] longTo8LenByteArr(long l) {
        byte[] bs = new byte[8];
        bs[0] = (byte) (l >> 56);
        bs[1] = (byte) (l >> 48);
        bs[2] = (byte) (l >> 40);
        bs[3] = (byte) (l >> 32);
        bs[4] = (byte) (l >> 24);
        bs[5] = (byte) (l >> 16);
        bs[6] = (byte) (l >> 8);
        bs[7] = (byte) l;
        return bs;
    }

    public static long bytes6LenToLong(byte[] bs) {
        return ((long) bs[0] & 0xff) << 40 | ((long) bs[1] & 0xff) << 32 | ((long) bs[2] & 0xff) << 24
                | ((long) bs[3] & 0xff) << 16 | ((long) bs[4] & 0xff) << 8 | ((long) bs[5] & 0xff);
    }

    public static long bytes8LenToLong(byte[] bs) {
        return ((long) bs[0] & 0xff) << 56 | ((long) bs[1] & 0xff) << 48 | ((long) bs[2] & 0xff) << 40
                | ((long) bs[3] & 0xff) << 32 | ((long) bs[4] & 0xff) << 24 | ((long) bs[5] & 0xff) << 16
                | ((long) bs[6] & 0xff) << 8 | ((long) bs[7] & 0xff);
    }

    public static int bytes4LenToInt(byte[] bs){
        return   bs[3] & 0xFF |
                (bs[2] & 0xFF) << 8 |
                (bs[1] & 0xFF) << 16 |
                (bs[0] & 0xFF) << 24;
    }

    private ProtocolUtil() {
    }
}
